package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.SetmealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealDao setmealDao;

    @Override
    public Result findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        Page<Setmeal> setmealPage=setmealDao.findPage(queryPageBean.getQueryString());

        PageResult pageResult = new PageResult();
        pageResult.setTotal(setmealPage.getTotal());
        pageResult.setRows(setmealPage.getResult());

        return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS,pageResult);
    }

    @Override
    public Result add(Setmeal setmeal, List<String> checkgroupIds) {
        setmealDao.add(setmeal);

        for (String checkgroupId : checkgroupIds) {
            setmealDao.addcheckgroupId(setmeal.getId(),checkgroupId);
        }
        return new Result(true, MessageConst.ADD_SETMEAL_SUCCESS,null);
    }
}
